package com.qingyun.modular.distributor.controller;

import com.github.pagehelper.Page;
import com.qingyun.common.annotion.DataSource;
import com.qingyun.common.constant.Const;
import com.qingyun.common.constant.DSEnum;
import com.qingyun.common.constant.factory.PageFactory;
import com.qingyun.common.controller.BaseController;
import com.qingyun.core.shiro.ShiroKit;
import com.qingyun.modular.model.Orders;
import com.qingyun.modular.order.service.IExportService;
import com.qingyun.modular.order.service.IOrdersService;
import com.qingyun.modular.order.vo.OrdersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

/**
 * 分销商控制器
 */
@Controller
@RequestMapping("/distributorlist")
public class DistributorController extends BaseController {
    @Autowired
    IOrdersService orderService;
    @Resource
    private IExportService exportService;

    private String prefix = "/distributor/";

    /**
     * 跳转到订单列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        return prefix + "/distributor.html";
    }

    /**
     * 分页获取订单列表
     * @param id 订单id
     * @param userId 分销商id
     * @param beginTime 订单开始时间
     * @param endTime 订单结束时间
     * @return
     */
    @RequestMapping(value = "/list")
    @DataSource(name = DSEnum.DATA_SOURCE_BIZ)//切换数据源
    @ResponseBody
    public Object list(String id,String userId,String beginTime,String endTime) {
        //分页对象信息封装
        Page page = new PageFactory<Orders>().defaultPage();
        //查询
        List<OrdersVo> list = orderService.selectList(id, userId, beginTime, endTime);
        long total = page.getTotal();
        //返回参数
        return super.packForBT(list,total);
    }

    /**
     * 查询导出
     * @param id 订单id
     * @param userId 分销商id
     * @param beginTime 订单开始时间
     * @param endTime 订单结束时间
     * @return
     */
    @DataSource(name = DSEnum.DATA_SOURCE_BIZ)//切换数据源
    @RequestMapping(value = "/selectListExport")
    public ResponseEntity<byte[]> selectListExport(HttpServletRequest request, HttpServletResponse response, String id, String userId, String beginTime, String endTime) throws ParseException {
        //查询
        List<OrdersVo> list = orderService.selectListExport(id, userId, beginTime, endTime);
        // 导出查询结果数据
        ResponseEntity<byte[]> result = exportService.exportOrdersExcel(request, response, list);
        return result;
    }
}
